package com.ftg.learn.psi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.psi.entity.Item;
import com.ftg.learn.psi.service.IItemService;
import com.ftg.learn.psi.utils.JsonEnum;
import com.ftg.learn.psi.utils.ResEntity;
import com.ftg.learn.psi.utils.ResPageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author guoBingTong
 * @since 2022-05-18
 */
@RestController
@RequestMapping("/psi/item")
public class ItemController {

    @Autowired
    IItemService iItemService;

    /**
     * 分页展示商品数据
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/page")
    public ResPageEntity page(@RequestParam int page, @RequestParam int limit){
        IPage<Item> iPage = new Page<>(page, limit);
        iItemService.page(iPage);
        return new ResPageEntity(iPage);
    }

    /**
     * 根据商品条码实现商品的查询
     * @param code
     * @return
     */
    @GetMapping("/findItemByCode/{code}")
    public ResEntity findItemByCode(@PathVariable("code") String code){
        List<Map<String, Object>> oneItemByCode = iItemService.findOneItemByCode(code);
        if (CollectionUtils.isEmpty(oneItemByCode)){
            return ResEntity.builder()
                    .code(JsonEnum.Shop_Err.getCode())
                    .msg(JsonEnum.Shop_Err.getMsg())
                    .build();

        }
        return new ResEntity(0,"success",oneItemByCode);
    }

    /**
     * 添加商品
     * @param item
     * @return
     */
    @PostMapping("/insert")
    public ResEntity insertItem(@RequestBody Item item){
        return iItemService.save(item) ? new ResEntity().OK() : new ResEntity().Failure();
    }
}
